<template>
  <div >
    <el-dialog
      :title="title"
      :visible.sync="dialogVisible"
      width="58rem"
      :before-close="tableClose"
    >
      <el-form
        :model="addInfo"
        status-icon
        :rules="rules"
        ref="addInfoRef"
        label-width="6.3rem"
      >
        <el-row>
          <el-col :span="11">
            <el-form-item label="屠宰批次"
            prop="AddIn.shBatch"
            :rules="{
                      required: true,
                      message: '不能为空',
                      trigger: 'blur',
                    }">
              <el-input
                v-model="addInfo.AddIn.shBatch"
                placeholder="请输入"
              ></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="11" :offset="1">
            <el-form-item label="屠宰场id" >
              <el-input
                v-model="addInfo.AddIn.slaId"
                disabled
              ></el-input>
            </el-form-item>
          </el-col>
        </el-row>
        <!-- 第二行 -->
         <el-row>
          <el-col :span="11">
            <el-form-item label="入库时间"
            prop="AddIn.date"
            :rules="{
                      required: true,
                      message: '不能为空',
                      trigger: 'blur',
                    }">
              <el-date-picker
               v-model="addInfo.AddIn.date"
              type="datetime"
              placeholder="选择日期时间">
            </el-date-picker>
            </el-form-item>
          </el-col>
          <el-col :span="11" :offset="1">
            <el-form-item label="经手人" prop="AddIn.hander"
            :rules="{
                      required: true,
                      message: '不能为空',
                      trigger: 'blur',
                    }">
              <el-input
                v-model="addInfo.AddIn.hander"
                placeholder="请输入"
              ></el-input>
            </el-form-item>
          </el-col>
        </el-row>

      <!-- 需要新增多个订单详情 -->
      <el-divider content-position="center">订单详情</el-divider>

          <el-table
          :data="addInfo.AddInI"
          style="width: 100%"
          size="mini"
          class="table"
          :row-style="{height:'1.2rem'}"
          :header-row-style="{height:'1.2rem'}"
          :cell-style="{padding:'0rem' }"
          >
         <el-table-column label="规格"
         width="156rem"
         header-align="center"
         >
					<template slot-scope="scope">
            <el-form-item
            label-width="0rem"
              :prop="
              'AddInI.' + scope.$index + '.type'"
              :rules='rules.type'>
              <el-input v-model="scope.row.type"
           ></el-input>
            </el-form-item>
					</template>
				</el-table-column>
        <el-table-column label="数量" width="156rem"
        header-align="center">
					<template slot-scope="scope">
						<el-form-item
            label-width="0rem"
              :prop="
              'AddInI.' + scope.$index + '.count'"
              :rules='rules.count'>
              <el-input v-model="scope.row.count"
           ></el-input>
            </el-form-item>
					</template>
				</el-table-column>

        <el-table-column label="单位" width="156rem" header-align="center">
					<template slot-scope="scope">
						<el-form-item
            label-width="0rem"
              :prop="
              'AddInI.' + scope.$index + '.unit'"
              :rules='rules.unit'>
              <el-input v-model="scope.row.unit"
           ></el-input>
            </el-form-item>
					</template>
				</el-table-column>

        <el-table-column label="备注" width="156rem" header-align="center">
					<template slot-scope="scope">
							<el-form-item
            label-width="0rem">
              <el-input v-model="scope.row.remark"
           ></el-input>
            </el-form-item>
					</template>
				</el-table-column>
        <el-table-column label="操作" width="70rem" header-align="center">
					<template slot-scope="scope">
            <el-form-item  label-width="0rem">
              <el-button size="mini" type="danger" @click="removeDomain(scope.$index)">删除</el-button>
            </el-form-item>

					</template>
				</el-table-column>
        </el-table>
      </el-form>
      <span slot="footer"
            class="dialog-footer">
          <el-button   @click="addDomain"
          type="success" plain>新增详情</el-button>
        <el-button @click="tableClose" >取 消</el-button>
        <el-button type="primary" @click="addOrderInfo('addInfo')">提 交</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import { AddOrder } from "@/api/Slaughter/ColdStorageIn.js";

export default {
  name: "InForm",
  props: {
    addInfo: {
      Object,
      default: {},
    },
    dialogVisible: {
      Boolean,
      default: false,
    },
    //表名
    title: {
      String,
      default: "",
    },
  },
  data() {
    return {

      //表单规则
      rules: {
      type:[{required: true, message: '不能为空',trigger: 'blur',}],
      count:[{required: true, message: '不能为空',trigger: 'blur',}],
      unit:[{required: true, message: '不能为空',trigger: 'blur',}],
      remark:[{required: true, message: '不能为空',trigger: 'blur',}],
      },
    };
  },
  methods: {
    //查询表格数据
    DisplayData() {
      this.$emit("DisplayData");
    },
    // 添加订单
    addOrderInfo() {
      this.$refs["addInfoRef"].validate(async (valid) => {
        if (valid) {
          console.log("新增提交的订单lll", this.addInfo);
          const { data: res } = await AddOrder(this.addInfo);
          console.log(res);
          if (res.statusCode === 20000) {
            this.$message.success("增加成功");
           this.closeform();
          } else {
            if (res.data == null) {
              return this.$message.error("添加失败");
            }
          }
        }
      });
    },

    //删除记录
    removeDomain(index) {
       this.addInfo.AddInI.splice(index, 1);
    },
    //弹窗新增详情记录
    addDomain() {
      this.addInfo.AddInI.push({
        count:"",
        type:"",
        unit:"",
        remark: "",
      });
    },

    //重置
    resetForm() {
      this.$nextTick(() => {
        this.$refs.addInfoRef.resetFields();
      });
    },
    //关闭表单
    closeform() {
      var a = false;
      this.$emit("closeAddDialog", a);
      this.DisplayData();
      this.resetForm();
    },

    // 关闭弹窗
    tableClose() {
      var a = false;
       this.$emit("closeAddDialogB", a);
       this.resetForm();
    },
  },
};
</script>

<style lang="scss" scoped>

</style>
